global base "G:\Survey_Methodology\People\Family Study\publications\external\Parenting Methods\Submission2\Dataverse\"
use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
cd "${base}\output_files"

encode RACE, gen(race)
gen race_string="other" if race==1
replace race_string="black" if race==2
replace race_string="hispanic" if race==3
replace race_string="other" if race==4
replace race_string="white" if race==5

**Restrict and standardize to adolescent pop
keep if age>=13
*****************

global neg_loadf1 "B10_14	B10_17	B10_15	B10_13"

drop if RACE=="Asian"
drop if RACE=="Multiracial or other race"
encode RACE, gen(rnum)

**REVERSE CODE negative loaders**
foreach  i in $neg_loadf1 {
gen RE`i'=5 if `i'==1
replace RE`i'=4 if `i'==2
replace RE`i'=3 if `i'==3
replace RE`i'=2 if `i'==4
replace RE`i'=1 if `i'==5
}

*Pooled--list of items in each scale
global f1 "B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7"
global f2 "REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18"
global f3 "B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe"
global f4 "C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol"

gen YC19=1 if C19_parent_love>=8 & C19_parent_love	!=.
replace YC19=0 if C19_parent_love<8
gen YC24_safety=1 if C24_safety==4 | C24_safety==5
replace YC24_safety=0 if C24_safety<4

foreach x in $f1 $f2 B6_overall_parent_ch B11B	REB11A	REB10_8	B9_relationship_othe {
	gen Y`x'=1 if `x'==4 | `x'==5
	replace Y`x'=0 if `x'<4
}

global bin1 "YB10_2 YB10_6 YB10_3 YB10_1 YB10_5 YB10_19 YB10_4 YB10_21 YB10_7"
global bin2 "YREB10_14 YREB10_17 YREB10_15 YREB10_13 YB10_16 YB10_18"
global bin3 "YB6_overall_parent_ch YB11B YREB11A YREB10_8 YC19  YB9_relationship_othe"
global bin4 "YC24_safety NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol"

**Mantel-Haenszel DIF**
**Binary outcomes
foreach x in bin1 bin2 bin3 bin4  {
difmh $`x' if black==1 | white==1, group(black) maxp(.05)
difmh $`x' if hispanic==1 | white==1, group(hispanic) maxp(.05)
difmh $`x' , group(white) maxp(.05)
}

**Group-specific latent constructs
****
egen rules_B=rowmean(B10_2	B10_4	B10_21	B10_6	B10_3	B10_1	B10_19	B10_5	B10_12	B10_16	B10_7)
egen enforce_B=rowmean(REB10_15	REB10_17	REB10_14	REB10_13)
egen relate_B=rowmean(B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe)
**USE same structure as White respondents and Black respondents
*gen adverse_B=.
egen adverse_B=rowmean(NC23a_rejection	NC21_angry	NC23_left	NC20_alcohol	C24_safety)

egen rules_H=rowmean(B10_2	B10_21	B10_1	B10_4	B10_6	B10_3	B10_7	B10_19)
egen enforce_H=rowmean(REB10_13	REB10_15	REB10_17	B10_16)
egen relate_H=rowmean(B6_overall_parent_ch	C19_parent_love	REB11A	B9_relationship_othe	REB10_8	B11B)
egen adverse_H=rowmean(NC23a_rejection	NC23_left	C24_safety	NC20_alcohol	NC21_angry)

egen rules_W=rowmean(B10_2	B10_5	B10_3	B10_6	B10_1	B10_19	B10_4	B10_21)
egen enforce_W=rowmean(B10_16	B10_18 REB10_14	REB10_17 REB10_15	REB10_13)
egen relate_W=rowmean(B6_overall_parent_ch	REB11A	REB10_8	B11B	C19_parent_love)
egen adverse_W=rowmean(NC23a_rejection	NC21_angry	NC23_left	NC20_alcohol	C24_safety)

*general measures across groups
egen rules=rowmean(B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7)
egen enforce=rowmean(REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18)
egen relate=rowmean(B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe)
egen adverse=rowmean(C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol)

foreach x in rules enforce relate adverse {
gen idio_`x'=`x'_B if rnum==1
replace idio_`x'=`x'_H if rnum==2
replace idio_`x'=`x'_W if rnum==3
gen SB_`x'=`x'*black
gen SH_`x'=`x'*hispanic
gen IB_`x'=idio_`x'*black
gen IH_`x'=idio_`x'*hispanic
}

label var idio_rules "Responsiveness"
label var idio_enforce "Demandingness"
label var idio_relate "Parent-child relationship quality"
label var idio_adverse "Absence of childhood traumatic experiences"
label var IB_rule "Responsiveness x Black"
label var IB_enforce "Demandness x Black"
label var IB_relate "Relationship Quality x Black"
label var IB_adverse "No Trauma x Black"
label var IH_rule "Responsiveness x Hispanic"
label var IH_enforce "Demandness x Hispanic"
label var IH_relate "Relationship Quality x Hispanic"
label var IH_adverse "No Trauma x Hispanic"
label var SB_rule "Responsiveness x Black"
label var SB_enforce "Demandness x Black"
label var SB_relate "Relationship Quality x Black"
label var SB_adverse "No Trauma x Black"
label var SH_rule "Responsiveness x Hispanic"
label var SH_enforce "Demandness x Hispanic"
label var SH_relate "Relationship Quality x Hispanic"
label var SH_adverse "No Trauma x Hispanic"

**Logistic DIF test
foreach x in bin1 bin2 bin3 bin4  {
diflogistic $`x' if black==1 | white==1, group(black) maxp(.05)
diflogistic $`x' if hispanic==1 | white==1, group(hispanic) maxp(.05)
diflogistic $`x' , group(white) maxp(.05)
}


*****
**More complex Logit model Tests for DIF, using group-specific latent constructs
******
global controls "married sole_caretake bio_parent parent_adopt foster_parent step_parent parent_age dad parent_hs_less parent_some male age_group_num1 age_group_num2 age_group_num3 age_group_num4 age_group_num5 age_group_num6 age_group_num7 age_group_num8"

estimates clear

sum YB10_2 YB10_6 YB10_3 YB10_1 YB10_5 YB10_19 YB10_4 YB10_21 YB10_7
logistic YB10_2 idio_rules black hispanic $controls [pw=weight]

foreach y in $bin1 {
eststo: logistic `y' black hispanic  idio_rules $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y' black hispanic IB_rules IH_rules idio_rules  $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}
foreach y in $bin2 {
eststo: logistic `y' black hispanic idio_enforce  $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y' black hispanic IB_enforce IH_enforce idio_enforce  $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}
foreach y in $bin3 {
eststo: logistic `y'  black hispanic idio_relate $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y'  black hispanic IB_relate IH_relate idio_relate $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}
foreach y in $bin4 {
eststo: logistic `y'  black hispanic idio_adverse  $controls  [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y'  black hispanic IB_adverse  IH_adverse idio_adverse $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}

esttab using "DIF_LOGIT_TEST_Items.csv", ///
stats(log_likelihood N, label("Log likelihood" "Sample Size")) ///
b(%15.3f) se(%15.3f) label  star(* 0.05 ** 0.01 *** 0.001)   nogaps replace eform

********
**Use same latent contructs
*********
global controls "married sole_caretake bio_parent parent_adopt foster_parent step_parent parent_age dad parent_hs_less parent_some male age_group_num1 age_group_num2 age_group_num3 age_group_num4 age_group_num5 age_group_num6 age_group_num7 age_group_num8"

estimates clear

sum YB10_2 YB10_6 YB10_3 YB10_1 YB10_5 YB10_19 YB10_4 YB10_21 YB10_7
logistic YB10_2 idio_rules black hispanic $controls [pw=weight]

foreach y in $bin1 {
eststo: logistic `y' black hispanic  rules $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y' black hispanic SB_rules SH_rules rules  $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}
foreach y in $bin2 {
eststo: logistic `y' black hispanic enforce  $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y' black hispanic SB_enforce SH_enforce enforce  $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}
foreach y in $bin3 {
eststo: logistic `y'  black hispanic relate $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y'  black hispanic SB_relate SH_relate relate $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}
foreach y in $bin4 {
eststo: logistic `y'  black hispanic adverse  $controls  [pw=weight]
estadd local log_likelihood `e(ll)', replace
eststo: logistic `y'  black hispanic SB_adverse  SH_adverse adverse $controls [pw=weight]
estadd local log_likelihood `e(ll)', replace
}

esttab using "GENERAL_DIF_LOGIT_TEST_Items.csv", ///
stats(log_likelihood N, label("Log likelihood" "Sample Size")) ///
b(%15.3f) se(%15.3f) label  star(* 0.05 ** 0.01 *** 0.001)   nogaps replace eform
